﻿using System;
using System.Text;
using System.Windows.Data;

namespace Eirene.WPF.Util
{
    /// <summary>
    /// Allows for concatenating several fields for a single property when DataBinding eg. a TextBlock
    /// or Label.
    /// See the <see cref="Convert"/>-method's summary for more information.
    /// </summary>
    /// <remarks>See class-code for a XAML-sample.</remarks>
    [ValueConversion(typeof(object), typeof(string))]
    public class ConcatenateFieldsMultiValueConverter : IMultiValueConverter
    {
        //Sample Usage:
        //<Window.Resources>
        //    <local:ConcatenateFieldsMultiValueConverter x:Key="concatenateFieldsMultiValueConverter"/>
        //</Window.Resources>
        //
        // (...)
        //
        //<TextBlock>
        //    <TextBlock.Text>
        //        <MultiBinding Converter="{StaticResource concatenateFieldsMultiValueConverter}"
        //                      ConverterParameter=", ">
        //            <Binding Path="LastName"/>
        //            <Binding Path="FirstName"/>
        //        </MultiBinding>
        //    </TextBlock.Text>
        //</TextBlock>

        /// <summary>
        /// Concatenates multiple bindings to a single string (eg. "[LastName], [Firstname]").
        /// The converter uses ", " as the default delimiter between the concatenated values.
        /// If a special delimiter is to be used, pass it (string type) in <paramref name="parameter">[parameter]</paramref>.
        /// </summary>
        /// <param name="values">Pass all bindings here.</param>
        /// <param name="targetType">(Irrelevant.)</param>
        /// <param name="parameter">Optionally pass the delimiter to be used here (default: ", ").</param>
        /// <param name="culture">(Irrelevant.)</param>
        /// <returns>
        /// All bindings concatenated into a single string, delimited by either ", " (default) 
        /// or the delimiter (string) passed in <paramref name="parameter"/>
        /// </returns>
        /// <remarks></remarks>
        public object Convert(
                    object[] values,
                    Type targetType,
                    object parameter,
                    System.Globalization.CultureInfo culture
                 )
        {
            string strDelimiter;
            StringBuilder sb = new StringBuilder();

            if (parameter != null)
            {
                //Use the passed delimiter.
                strDelimiter = parameter.ToString();
            }
            else
            {
                //Use the default delimiter.
                strDelimiter = ", ";
            }

            //Concatenate all fields
            foreach (object value in values)
            {
                if (value != null && value.ToString().Trim().Length > 0)
                {
                    if (sb.Length > 0) sb.Append(strDelimiter);
                    sb.Append(value.ToString());
                }
            }

            return sb.ToString();
        }

        public object[] ConvertBack(
                    object value,
                    Type[] targetTypes,
                    object parameter,
                    System.Globalization.CultureInfo culture
              )
        {
            throw new NotImplementedException("ConcatenateFieldsMultiValueConverter cannot convert back (bug)!");
        }
    }
}
